#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "DevicesTime.h"
#include "cpuusage.h"


/* us */
#define CPU_USAGE_CALC_TICK    100000ll

/* CPU 使用率 */
float g_fCPUUsage = 0.0f;


void vCPUUsageIdleHook(void)
{
    static int64_t st_lCountMax = 0;
    int64_t lTickStop = 0, lTickNow = 0;

    lTickStop = lTimeGetStamp() + CPU_USAGE_CALC_TICK;
    while(lTimeGetStamp() < lTickStop)
    {
        ++lTickNow;
    }

    st_lCountMax = st_lCountMax < lTickNow ? lTickNow : st_lCountMax;

    /* get CPU usage */
    lTickNow = st_lCountMax - lTickNow;
    g_fCPUUsage = (float)lTickNow / (float)st_lCountMax;
}

float fCPUUsageGet(void)
{
    return g_fCPUUsage;
}
